package com.vodone.v1pin.component.dropdown
{
	import caurina.transitions.Tweener;
	
	import com.vodone.v1pin.component.dropdown.events.SelectEvent;
	import com.vodone.v1pin.core.event.V1webEventDispatcher;
	import com.vodone.v1pin.detail.events.SendDataEvent;
	import com.vodone.v1pin.userInfo.event.CloseWindowEvent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.clearTimeout;
	import flash.utils.getTimer;
	import flash.utils.setTimeout;
	
	import mx.binding.utils.BindingUtils;
	import mx.containers.Canvas;
	import mx.containers.HBox;
	import mx.controls.Button;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.controls.Spacer;
	import mx.core.UIComponent;
	import mx.effects.Move;
	import mx.effects.easing.Elastic;
	import mx.events.EffectEvent;
	import mx.events.InvalidateRequestData;
	import mx.events.ResizeEvent;
	import mx.managers.PopUpManager;
	
	[DefaultProperty("dropdownContent")]
	public class DropdownButton extends DropContainer
	{
		public function DropdownButton()
		{
			super();
//			addEventListener( Event.ADDED_TO_STAGE , addToStageHandler );
			addEventListener( Event.REMOVED_FROM_STAGE , removeFromStageHandler );
			addEventListener( MouseEvent.MOUSE_OVER , mouseOverHandler );
			addEventListener( MouseEvent.MOUSE_OUT , mouseOutHandler );
			addEventListener( MouseEvent.CLICK , clickHandler );
			V1webEventDispatcher.getInstance().addEventListener(CloseWindowEvent.REFRESH_NAV_EVENT,refreshNav);
			V1webEventDispatcher.getInstance().addEventListener(CloseWindowEvent.CLOSE_NAV_EVENT,navResizeHandler);
			V1webEventDispatcher.getInstance().addEventListener(CloseWindowEvent.EXTENDS_NAV_EVENT,navResizeHandler);
			V1webEventDispatcher.getInstance().addEventListener(CloseWindowEvent.CLOSE_WINDOW_EVENT,closeDropdown2);
		}
		
		private var interVal:Number;
		private function refreshNav(e:CloseWindowEvent):void
		{
			if(interVal){
				clearTimeout(interVal);
			}
			interVal = setTimeout(setDropdownPosition,100);
		}
		
		public var useClick:Boolean = false;
		private var closeBySelf:Boolean = false;
		private function clickHandler(e:MouseEvent):void
		{
			if( useClick )
			{
				dropdownOpen = !dropdownOpen;
				dropdownContent.addEventListener(MouseEvent.CLICK , dropdownClickHandler )
				invalidateDropDown();
			}
		}
		private function dropdownClickHandler(e:MouseEvent):void
		{
			closeDropdown();
		}
		
		/**
		 * 是否处理鼠标划入划出，主要用于全部展开的情况 
		 */		
		private var enableMouseOverOut:Boolean = true;
		private var txt:Label;
		public var useSpacer:Boolean = true;
		protected override function createChildren():void
		{
			super.createChildren();
			txt = new Label();
			txt.buttonMode = true;
			txt.mouseChildren = false;
			addChild(txt);
			BindingUtils.bindProperty( txt , "text" , this , "label" );
			
			if( useSpacer )
			{
				var spacer:Spacer = new Spacer();
				spacer.percentWidth = 100;
				addChild( spacer );
			}
			
			var image:Image = new Image();
			image.source = "assets/detail/san_jiao.png";
			addChild(image)
		}
		private var haveSelected:Boolean = false;
		private var selectUI:UIComponent;
		public function addSelect( ui:UIComponent , p:Point = null ):void
		{
			selectUI = ui;
			ui.addEventListener( "removeSelected" ,  removeSelectedHandler );
			if( p != null )
			{
				var point:Point = this.localToGlobal( new Point( txt.x + txt.width + 3 , ( height - selectUI.height ) / 2 ) );
				selectUI.x = p.x;
				selectUI.y = p.y;
				PopUpManager.addPopUp( selectUI , this  );
				var move:Move = new Move( selectUI );
				move.duration = 300;
				move.yFrom = selectUI.y;
				move.xFrom = selectUI.x;
				move.yTo = point.y;
				move.xTo = point.x;
				move.addEventListener(EffectEvent.EFFECT_END , effectEndHandler  );
				move.play();
			}
			else
			{
				addSelectUI();
			}
		}
		
		private function effectEndHandler(e:Event):void
		{
			addSelectUI();
		}
		
		private function addSelectUI():void
		{
			if( selectUI )
			{
				PopUpManager.removePopUp( selectUI );
				addChildAt( selectUI ,numChildren - 2 );
			}
		}
		private function removeSelectedHandler( e:Event ):void
		{
			var ui:UIComponent = e.currentTarget as UIComponent;
			removeSelected(ui)
		}
		public function removeSelected(ui:UIComponent):void
		{
			if( ui && ui.parent == this  )
			{
				removeChild( ui );
				dropdownEnabled = true;
			}
		}
		
		private function addToStageHandler( e:Event ):void
		{
			

		}
		
		private function navResizeHandler(e:CloseWindowEvent):void
		{
			if( dropdownOpen )
			{
				var pos:int = 95;
				if( e.type == CloseWindowEvent.CLOSE_NAV_EVENT )
				{
					pos = -95;
				}
				setDropdownPosition( pos );
			}
		}
		private var dropDownDrity:Boolean = false;
		private function invalidateDropDown():void
		{
			dropDownDrity = true;
			invalidateProperties();
		}
		private var mouseOn:Boolean = false;
		private function mouseOverHandler( e:MouseEvent ):void
		{
			mouseOn = true;
			if( enableMouseOverOut && !useClick && dropdownEnabled )
			{
				dropdownOpen = true;
				setTimeout( function():void{
					if( mouseOn )
					{
						invalidateDropDown();
					}
				} , 300 );
				
			}
//			trace( "mouse over: target:" + e.target + ";time:" + getTimer() )
		}
		private function mouseOutHandler( e:MouseEvent ):void
		{
			mouseOn = false;
			if( enableMouseOverOut && !useClick)
			{
				dropdownOpen = false;
				setTimeout( function():void{
					if( !dropdownOpen )
					{
						invalidateDropDown();
					}
				} , 50 );
			}
//			trace( "mouse out: target:" + e.target + ";time:" + getTimer() )
		}
		
		private var dropdownInterval:Number;
		protected override function commitProperties():void
		{
			super.commitProperties();
			if( dropDownDrity )
			{
				dropDownDrity = false;
				if( dropdownOpen ) 
				{
					/*if(dropdownInterval){
						clearTimeout(dropdownInterval);
					}
					dropdownInterval = setTimeout(openDropdown,1000);*/
					openDropdown();
//					trace("open:" + getTimer());
				}
				else
				{
					closeDropdown();
//					trace("close:" + getTimer());
				}
			}
		}
		private function removeFromStageHandler( e:Event ):void
		{
//			removeEventListener( Event.REMOVED_FROM_STAGE , removeFromStageHandler );
//			removeEventListener( MouseEvent.MOUSE_OVER , mouseOverHandler );
//			removeEventListener( MouseEvent.MOUSE_OUT , mouseOutHandler );
//			V1webEventDispatcher.getInstance().removeEventListener(CloseWindowEvent.CLOSE_NAV_EVENT,navResizeHandler);
//			V1webEventDispatcher.getInstance().removeEventListener(CloseWindowEvent.EXTENDS_NAV_EVENT,navResizeHandler);
		}
		
		private var dropdownOpen:Boolean = false;
		public function openDropdown( dropDownHeight:Number = 0 , openAll:Boolean = false ):void
		{
			if( dropdownEnabled || openAll )
			{
				dropdownOpen = true;
				if( null == dropdownContent )
				{
					dropdownContent = new Canvas();
					dropdownContent.height = this.width;
					dropdownContent.setStyle( "backgroundColor" , 0xff0000 );
				}
				setDropdownPosition();
				if( dropDownHeight > 0 )
				{
					dropdownContent.height = dropDownHeight;
				}
				enableMouseOverOut = !openAll;
//				this.addChild( dropdownContent );
//				dropdownContent.includeInLayout = false;
				PopUpManager.addPopUp( dropdownContent , this  );
			}
		}
		
		private function endEffect():void
		{
			Tweener.addTween( dropdownContent,{alpha:1,time:.5,transition:"easeOutQuint"} );
		}
		public function setDropdownPosition( pos:int = 0 ):void
		{
			var gPoint:Point = localToGlobal( new Point( 0 , height + pos ) );
			if( pos != 0 )
			{
				Tweener.addTween( dropdownContent,{y:gPoint.y,time:.5,transition:"easeOutQuint"} );
				Tweener.addTween( dropdownContent,{alpha:0,time:.5,transition:"easeOutQuint",onComplete:endEffect} );
			}
			else
			{
				dropdownContent.x = gPoint.x;
//				dropdownContent.y = gPoint.y;
				Tweener.addTween( dropdownContent,{y:gPoint.y,time:.5,transition:"easeOutQuint"} );
				dropdownContent.width = this.width;
			}
		}
			
		public function closeDropdown2( closeAll:Boolean = false ):void
		{
			dropdownOpen = false;
			if( null != dropdownContent )
			{
				PopUpManager.removePopUp( dropdownContent   );
			}
		}
		public function closeDropdown( closeAll:Boolean = false ):void
		{
			enableMouseOverOut = !closeAll;
			dropdownOpen = false;
			if( null != dropdownContent )
			{
				PopUpManager.removePopUp( dropdownContent   );
			}
		}
		private var _dropdownContent:UIComponent;

		public function get dropdownContent():UIComponent
		{
			return _dropdownContent;
		}

		public function set dropdownContent(value:UIComponent):void
		{
			if( value != _dropdownContent )
			{
				_dropdownContent = value;
				_dropdownContent.addEventListener( MouseEvent.MOUSE_OVER , mouseOverHandler );
				_dropdownContent.addEventListener( MouseEvent.MOUSE_OUT , mouseOutHandler );
				_dropdownContent.addEventListener( SelectEvent.SELECT , selectHandler );
				this.addEventListener( SelectEvent.UN_SELECT , unSelectHandler );
				BindingUtils.bindProperty( _dropdownContent , "width" , this , "width" ); 
			}
		}
		private function unSelectHandler(e:SelectEvent):void
		{
			dropdownEnabled = true;
			if( !enableMouseOverOut )
			{
				openDropdown( 0 , !enableMouseOverOut  );
			}
		}
		private var dropdownEnabled:Boolean = true;
		protected function selectHandler( e:SelectEvent ):void
		{
			dropdownEnabled = enableMouseOverOut ? false : true;
			if( enableMouseOverOut )
			{
				closeDropdown( !enableMouseOverOut  );
			}
		}
	}
}